const db = wx.cloud.database();
// pages/class/class.js
Page({

	/**
	 * 页面的初始数据
	 */

	data: {
		leftList: [],
		rightList: [],
		/* 被点击菜单 */
		currentIndex: 0,
		/* 右侧滚动条距离 */
		scrollTop:0
	},
	classList1: [],

	onShow(e){
		console.log(e);
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
	const classList1 = wx.getStorageSync("cl1");
		if(!classList1){
			this.getClassList();
		}else{
			if(Date.now() - classList1.time > 1000*300){
				this.getClassList();
			}else{
				this.classList1 = classList1.data;
				let leftList = this.classList1.map(v => v.cat_name);
				let rightList = this.classList1[0].children;
				this.setData({
					leftList,
					rightList			
				})
			}
		}
		
		
	},

	async getClassList() {
		const res = await db.collection('nht_class_1').where({}).get()
			.then(result => {
				this.classList1 = result.data[0].message;
				
				/* 数据存入本地 */
				wx.setStorageSync("cl1",{time:Date.now(),data:this.classList1})
				
				let leftList = this.classList1.map(v => v.cat_name);
				let rightList = this.classList1[0].children;
				this.setData({
					leftList,
					rightList				
				})
				wx.stopPullDownRefresh();
			});
	},
	handleItemTap(e){
		let rightList = this.classList1[e.currentTarget.dataset.itemindex].children;
		this.setData({
			currentIndex: e.currentTarget.dataset.itemindex,
			rightList,
		    scrollTop:0	
		});

	}

})
